package demo.service;

import demo.dao.TopDao;
import demo.entity.employees;
import demo.entity.managers;
import demo.entity.record;
import demo.entity.users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;


import java.time.LocalDate;
import java.time.Period;
import java.util.List;

@Service
public class TopService {

    @Autowired
    private TopDao topDao;
    public users addUsers(users user) {  //添加employees
        return topDao.addUsers(user);
    }
    public employees addEmployees(employees emp) {  //添加employees
        return topDao.addEmployees(emp);
    }

    public void dropEmployeesById(int id) {   //删除employees
        topDao.dropEmployeesById(id);
    }
    public List<employees> queryAllEmployees(){
        return topDao.queryAllEmployees();
    }

    public employees queryEmployeesById(int id) {  //查询employees
        try {
            return topDao.queryEmployeesById(id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
    public employees changeRestHoliday(int emp_id, int newRestHoliday){
        return topDao.changeRestHoliday(emp_id,newRestHoliday);
    }
    public employees changeBasic_Salary(int emp_id, int newBasic_Salary){
        return topDao.changeBasic_Salary(emp_id,newBasic_Salary);
    }
    public employees changeMonthly_Salary(int emp_id, int newMonthly_Salary){
        return topDao.changeMonthly_Salary(emp_id,newMonthly_Salary);
    }
    public employees changeDepartment_id(int emp_id, String newDepartment_id){
        return topDao.changeDepartment_id(emp_id,newDepartment_id);
    }
    public employees changeJob_id(int emp_id, String newJob_id){
        return topDao.changeJob_id(emp_id,newJob_id);
    }
    public employees changeArrival_time(int emp_id, String newArrival_time){
        return topDao.changeArrival_time(emp_id,newArrival_time);
    }
    public employees changeDeparture_time(int emp_id, String newDeparture_time){
        return topDao.changeDeparture_time(emp_id,newDeparture_time);
    }
    public List<record> queryAllRecord() {
        return topDao.queryAllRecord();
    }
    public List<managers> queryManagers(){
        return topDao.queryManagers();
    }
    public managers queryManagersById(int id){
        return topDao.queryManagersById(id);
    }
    public managers dealManagers(int id,String status){
        if(status.equals("通过")){

            LocalDate start = LocalDate.parse(queryManagersById(id).getStart_date());
            LocalDate end = LocalDate.parse(queryManagersById(id).getEnd_date());
            Period p = Period.between(start,end);
            int interval = 365 * p.getYears() + 30 * p.getMonths() + p.getDays();

            changeRestHoliday(queryEmployeesById(queryManagersById(id).getEmp_id()).getEmp_id(),queryEmployeesById(queryManagersById(id).getEmp_id()).getRest_holiday()-interval);
        }
        return topDao.dealManagers(id,status);
    }
//    public List<Student> queryAllStudents() {
//        return studentDao.queryAllStudents();
//    }
//
//    public Student queryStudentById(int id) {
//        return studentDao.queryStudentById(id);
//    }
//
//    public Student insertStudent(Student student) {
//        return studentDao.insertStudent(student);
//    }
//
//    public Student updateStudentById(int id, Student student) {
//        return studentDao.updateStudentById(id, student);
//    }
//
//    public void deleteStudentById(int id) {
//        studentDao.deleteStudentById(id);
//    }
}
